<?php
	include_once("common.php");
	include_once("script/event.php");
	include_once("script/event_setting.php");

	if(isVisitor() || ! isset($_GET['id']) || ! findEvent($_GET['id'])){
		header("Location: index.php");
		exit(0);
	}

	$prog = isProgramChair($_SESSION['idusers'], $_GET['id']);
	$comm = isCommittee($_SESSION['idusers'], $_GET['id']);
	$part = isParticipant($_SESSION['idusers'], $_GET['id']);
	$mile = getMilestone($_GET['id']);

	#if(! $prog){
	#	header("Location: event.php?id=".$_GET['id']);
	#	exit(0);
	#}

	if(sizeof($_POST)>0){
		$result = true;
		foreach($_POST as $idarticle => $rate){
			$bid = $bid % 6;
			$result &= rating($idarticles, $_GET['id'], $rate);
			if(!$result){
				echo $idarticle, $bid;
			}
		}
	}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>COMP 353 Project</title>

    <!-- Bootstrap core CSS -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="./css/template.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy this line! -->
    <!--[if lt IE 9]><script src="../../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
</head>
<body>
	<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
		<div class="container">
			<div class="navbar-header">
				<a class="brand" href="#">SEMS Lite</a>
			</div>
			<div class="navbar">
				<ul class="nav navbar-nav">
					<li><a href="index.php">Home</a></li>
					<li><a href="search.php?<?php echo 'id='.$_GET['id']; ?>">Search</a></li>
					<li><a href="signup.php">Signup</a></li>
				</ul>
<?php
	if(isVisitor()){
		echo '<form class="navbar-form pull-right" method="post" action="index.php">'."\n",
			'<input name="username" type="text" placeholder="Username" class="form-control">'."\n",
			'<input name="password" type="password" placeholder="Password" class="form-control">'."\n",
			'<button type="submit" class="btn">Sign in</button>'."\n",
			'</form>'."\n";
	}
	else{
		echo '<form class="navbar-form pull-right" method="post" action="index.php">'."\n",
			'<input type="submit" class="btn" name="logout" value="Logout">'."\n",
			'</form>'."\n";
	}
?>
			</div>
		</div>
	</div>
<!-- CONTENT -->
<?php
	# Administrator menu
	if(isset($_SESSION['username']) && isAdmin($_SESSION['username']))
		include("script/admin_menu.php");

	if(!isVisitor())
		submenu($prog, $comm, $part, $mile, "rate");

	if(isset($result)){
		if($result)
			echo '<div class="alert alert-success">Saved</div>';
		else
			echo '<div class="alert alert-error">Invalid Submission</div>';
	}

	$commitee = loadCommitee($_GET['id']);
	$event_data = loadEvent($_GET['id']);
?>
	<div class="container">
		<legend>Event Settings</legend>
		<form class="form-horizontal" method="post" action="rating.php">
			<div class="control-group">
				<label class="control-label" for="inputDescription">Description</label>
				<div class="controls">
					<textarea name="description" id="inputDescription" placeholder="Description" row="5">
<?php
	echo $event_data['description'];
?>
					</textarea>
				</div>
			</div>
				<div class="control-group">
					<label class="control-label" for="dl_sub_id">End Submission Date</label>
					<div class="controls">
						<input type="date" id="dl_sub_id" name="dl_submissions" placeholder="yyyy-mm-dd" value="<?php echo $event_data['dl_submissions']; ?>"/>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="dl_bid_id">End Bidding Date</label>
					<div class="controls">
						<input type="date" id="dl_bid_id" name="dl_bidding" placeholder="yyyy-mm-dd" value="<?php echo $event_data['dl_bidding']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="dl_rat_id">End Rating Date</label>
					<div class="controls">
						<input type="date" id="dl_rat_id" name="dl_rating" placeholder="yyyy-mm-dd" value="<?php echo $event_data['dl_ratings']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="dl_end_id">End Event Date</label>
					<div class="controls">
						<input type="date" id="dl_end_id" name="dl_end" placeholder="yyyy-mm-dd" value="<?php echo $event_data['end']; ?>" />
					</div>
				</div>
			<div class="controls">
				<button class="btn" type="submit" >Save</button>
			</div>
		</form>
	</div>
	<div class="container">
		<legend>Comittee Members</legend>
		<table class="table" id="comitteeTable">
<?php
	function printComitte($data){
		echo '<tr>
			<td>'.$data['username'].'</td><td>'.$data['first_name'].'</td><td>'.$data['last_name'].'</td>
			</tr>';
	}

	foreach($commitee as $member){
		printComitte($member);
	}
?>
			<tr><td><input type="text" autocomplete="off" data-provide="typeahead" data-items="4" id="inputComitteeName" placeholder="Username"></td><td><button class="btn" id="addComittee">Add</button></td><td></td></tr>
		</table>
		<a href="topic_selector.php?id=<?php echo $_GET['id']; ?>" class="btn">Choose Topics</a>
	</div>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
	<!-- CDN -->
	<!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> -->
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
	<!-- LOCAL -->
    <script src="./js/jquery-1.10.2.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
	<script type="text/javascript">
	$("#addComitee").click(function(){
		var c_name = $("#addComittee").val();
		if(c_name != ""){
			$.ajax({url: "script/event_setting_addComittee.php", data: {name: c_name,
				 id: "<?php echo $_GET['id']; ?>"
			}})
				.done(function(reply){
					var data = reply.split(",");
					$("#comitteeTable").append(
						'<tr>'+
						'<td>'+$data[1]+'</td><td>'+$data[2]+'</td><td>'+$data[3]+'</td>'+
						'</tr>'
					);
				});
		}
	});

	$("#inputComitteeName").typeahead({source: function(txt, cb){
		if(txt.length> 2){
			$.ajax({url: "script/event_setting_getUser.php", data: {name: txt}}).
				done(function(reply){
					var data = reply.split(",");
					cb(data);
				});
		}
	}});
	</script>
  </body>
</html>
